package edu.henu.personnel.service.teacher;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.henu.personnel.constant.ErrorConstant;
import edu.henu.personnel.constant.PageHelperConstant;
import edu.henu.personnel.dao.TeacherDao;
import edu.henu.personnel.entity.Teacher;
import edu.henu.personnel.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: PersonnelProject
 * @description: 优秀教师人才实现类
 * @author: zhuyang
 * @create: 2019-05-05 15:19
 */

@Transactional
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherDao teacherDao;

    @Override
    public PageInfo<Teacher> getAllTeacherInfo(Integer pageNum) {
        PageHelper.startPage(pageNum, PageHelperConstant.afterTeacherSize);
        List<Teacher> teacherList = teacherDao.getAllTeacher();
        PageInfo<Teacher> pageInfo = new PageInfo<>(teacherList);
        return pageInfo;
    }

    @Override
    public List<Teacher> getTeacherInTheTelnetInfo(Integer ttid) {
        if (null == ttid)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);

        return teacherDao.getTeacherListByTtid(ttid);
    }

    @Override
    public Teacher getTeacherById(String tid) {
        if (StringUtils.isBlank(tid))
            throw BusinessException.withErrorCode(ErrorConstant.Auth.USERNAME_PASSWORD_IS_EMPTY);

        return teacherDao.getTeacherByTid(tid);
    }

    @Override
    public int updateTeacherInfo(Teacher teacher) {
        if (null == teacher)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);

        int result = teacherDao.updateTeacher(teacher);
        return result;
    }

    @Override
    public int addTeacherInfo(Teacher teacher) {
        if (null == teacher)
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);

        int result = teacherDao.insertTeacher(teacher);
        return result;
    }

    @Override
    public int deleteTeacherById(String tid) {
        if (StringUtils.isBlank(tid))
            throw BusinessException.withErrorCode(ErrorConstant.Auth.USERNAME_PASSWORD_IS_EMPTY);

        int result = teacherDao.deleteTeacher(tid);
        return result;
    }

    @Override
    public int addTeacherBrowse(String tid) {
        return teacherDao.addBrowses(tid);
    }
}
